home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1998 November / Freeware November 1998.img / dist / fw_UDELxntp.idb / usr / freeware / src / xntp-3.4o-export / authstuff / md5driver.c.z / md5driver.c
C/C++ Source or Header  |  1998-01-21  |  6KB  |  212 lines

  1. /*
  2.  ***********************************************************************
  3.  ** md5driver.c -- sample test routines                               **
  4.  ** RSA Data Security, Inc. MD5 Message-Digest Algorithm              **
  5.  ** Created: 2/16/90 RLR                                              **
  6.  ** Updated: 1/91 SRD                                                 **
  7.  ** Updated: 7/91 SRD Removed file "foo" from test suite              **
  8.  ***********************************************************************
  9.  */
  10.  
  11. /*
  12.  ***********************************************************************
  13.  ** Copyright (C) 1990, RSA Data Security, Inc. All rights reserved.  **
  14.  **                                                                   **
  15.  ** RSA Data Security, Inc. makes no representations concerning       **
  16.  ** either the merchantability of this software or the suitability    **
  17.  ** of this software for any particular purpose.  It is provided "as  **
  18.  ** is" without express or implied warranty of any kind.              **
  19.  **                                                                   **
  20.  ** These notices must be retained in any copies of any part of this  **
  21.  ** documentation and/or software.                                    **
  22.  ***********************************************************************
  23.  */
  24.  
  25. #include <stdio.h>
  26. #include <sys/types.h>
  27. #include <time.h>
  28. #if defined(SYS_BSDI) || defined(SYS_44BSD)
  29. #include <sys/time.h>
  30. #endif    /* SYS_BSDI */
  31. #include "md5.h"
  32.  
  33. #ifndef MD5
  34. #define    MD5
  35. #endif
  36. #include "ntp_string.h"
  37. #include "ntp_stdlib.h"
  38.  
  39. /* Prints message digest buffer in mdContext as 32 hexadecimal digits.
  40.    Order is from low-order byte to high-order byte of digest.
  41.    Each byte is printed with high-order hexadecimal digit first.
  42.  */
  43. static void
  44. MDPrint (mdContext)
  45. MD5_CTX *mdContext;
  46. {
  47.   int i;
  48.  
  49.   for (i = 0; i < 16; i++)
  50.     printf ("%02x", mdContext->digest[i]);
  51. }
  52.  
  53. /* size of test block */
  54. #define TEST_BLOCK_SIZE 1000
  55.  
  56. /* number of blocks to process */
  57. #define TEST_BLOCKS 10000
  58.  
  59. /* number of test bytes = TEST_BLOCK_SIZE * TEST_BLOCKS */
  60. static long TEST_BYTES = (long)TEST_BLOCK_SIZE * (long)TEST_BLOCKS;
  61.  
  62. /* A time trial routine, to measure the speed of MD5.
  63.    Measures wall time required to digest TEST_BLOCKS * TEST_BLOCK_SIZE
  64.    characters.
  65.  */
  66. static void
  67. MDTimeTrial ()
  68. {
  69.   MD5_CTX mdContext;
  70.   time_t endTime, startTime;
  71.   unsigned char data[TEST_BLOCK_SIZE];
  72.   unsigned int i;
  73.  
  74.   /* initialize test data */
  75.   for (i = 0; i < TEST_BLOCK_SIZE; i++)
  76.     data[i] = (unsigned char)(i & 0xFF);
  77.  
  78.   /* start timer */
  79.   printf ("MD5 time trial. Processing %ld characters...\n", (long)TEST_BYTES);
  80.   time (&startTime);
  81.  
  82.   /* digest data in TEST_BLOCK_SIZE byte blocks */
  83.   MD5Init (&mdContext);
  84.   for (i = TEST_BLOCKS; i > 0; i--)
  85.     MD5Update (&mdContext, data, TEST_BLOCK_SIZE);
  86.   MD5Final (&mdContext);
  87.  
  88.   /* stop timer, get time difference */
  89.   time (&endTime);
  90.   MDPrint (&mdContext);
  91.   printf (" is digest of test input.\n");
  92.   printf
  93.     ("Seconds to process test input: %ld\n", (long)endTime-startTime);
  94.   printf
  95.     ("Characters processed per second: %ld\n",
  96.      (long)(TEST_BYTES/(endTime-startTime)));
  97. }
  98.  
  99. /* Computes the message digest for string inString.
  100.    Prints out message digest, a space, the string (in quotes) and a
  101.    carriage return.
  102.  */
  103. static void
  104. MDString (inString)
  105. char *inString;
  106. {
  107.   MD5_CTX mdContext;
  108.   unsigned int len = strlen (inString);
  109.  
  110.   MD5Init (&mdContext);
  111.   MD5Update (&mdContext, inString, len);
  112.   MD5Final (&mdContext);
  113.   MDPrint (&mdContext);
  114.   printf (" \"%s\"\n", inString);
  115. }
  116.  
  117. /* Computes the message digest for a specified file.
  118.    Prints out message digest, a space, the file name, and a carriage
  119.    return.
  120.  */
  121. static void
  122. MDFile (filename)
  123. char *filename;
  124. {
  125.   FILE *inFile = fopen (filename, "rb");
  126.   MD5_CTX mdContext;
  127.   int bytes;
  128.   unsigned char data[1024];
  129.  
  130.   if (inFile == NULL) {
  131.     printf ("%s can't be opened.\n", filename);
  132.     return;
  133.   }
  134.  
  135.   MD5Init (&mdContext);
  136.   while ((bytes = fread (data, 1, 1024, inFile)) != 0)
  137.     MD5Update (&mdContext, data, bytes);
  138.   MD5Final (&mdContext);
  139.   MDPrint (&mdContext);
  140.   printf (" %s\n", filename);
  141.   fclose (inFile);
  142. }
  143.  
  144. /* Writes the message digest of the data from stdin onto stdout,
  145.    followed by a carriage return.
  146.  */
  147. static void
  148. MDFilter ()
  149. {
  150.   MD5_CTX mdContext;
  151.   int bytes;
  152.   unsigned char data[16];
  153.  
  154.   MD5Init (&mdContext);
  155.   while ((bytes = fread (data, 1, 16, stdin)) != 0)
  156.     MD5Update (&mdContext, data, bytes);
  157.   MD5Final (&mdContext);
  158.   MDPrint (&mdContext);
  159.   printf ("\n");
  160. }
  161.  
  162. /* Runs a standard suite of test data.
  163.  */
  164. static void
  165. MDTestSuite ()
  166. {
  167.   printf ("MD5 test suite results:\n");
  168.   MDString ("");
  169.   MDString ("a");
  170.   MDString ("abc");
  171.   MDString ("message digest");
  172.   MDString ("abcdefghijklmnopqrstuvwxyz");
  173.   MDString
  174.     ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
  175.   MDString
  176.     ("12345678901234567890123456789012345678901234567890123456789012345678901234567890");
  177. }
  178.  
  179. void
  180. main (argc, argv)
  181. int argc;
  182. char *argv[];
  183. {
  184.   int i;
  185.  
  186.   /* For each command line argument in turn:
  187.   ** filename          -- prints message digest and name of file
  188.   ** -sstring          -- prints message digest and contents of string
  189.   ** -t                -- prints time trial statistics for 10M
  190.                           characters
  191.   ** -x                -- execute a standard suite of test data
  192.   ** (no args)         -- writes messages digest of stdin onto stdout
  193.   */
  194.   if (argc == 1)
  195.     MDFilter ();
  196.   else
  197.     for (i = 1; i < argc; i++)
  198.       if (argv[i][0] == '-' && argv[i][1] == 's')
  199.         MDString (argv[i] + 2);
  200.       else if (strcmp (argv[i], "-t") == 0)
  201.         MDTimeTrial ();
  202.       else if (strcmp (argv[i], "-x") == 0)
  203.         MDTestSuite ();
  204.       else MDFile (argv[i]);
  205. }
  206.  
  207. /*
  208.  ***********************************************************************
  209.  ** End of md5driver.c                                                **
  210.  ******************************** (cut) ********************************
  211.  */
  212.